#include "StdAfx.h"
#include "HitKey.h"
#include "SeqInput.h"

namespace KeyAss 
{

	void HitKey::KeyDown(BYTE vk, DWORD flag)
	{
		_flag = flag;
		_scanCode = ::MapVirtualKey(vk, 0);

		INPUT Event = { 0 };
		Event.type = INPUT_KEYBOARD;
		Event.ki.dwExtraInfo = (ULONG_PTR)_sender->GetKey();
		Event.ki.dwFlags = KEYEVENTF_SCANCODE | _flag;
		Event.ki.wScan = _scanCode;
		_sender->GetEQ().push_back( Event );
	}

	void HitKey::KeyUp()
	{
		if(!_scanCode) return;
		INPUT Event = { 0 };
		Event.type = INPUT_KEYBOARD;
		Event.ki.dwExtraInfo = (ULONG_PTR)_sender->GetKey();
		Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP | _flag;
		Event.ki.wScan = _scanCode;
		_sender->GetEQ().push_back( Event );
	}

	HitKey::HitKey(SeqInput* sender)
	{
		_scanCode = 0;
		_sender = sender;
	}

	HitKey::~HitKey()
	{
		KeyUp();
	}


	CharKey::CharKey(SeqInput* sender, char c) :_shift(sender),  HitKey(sender)
	{
		SHORT svk = VkKeyScanEx(c, _sender->GetHKL());
		if(HIBYTE( svk ) == 1) _shift.KeyDown(VK_SHIFT, 0);
		KeyDown(LOBYTE(svk), 0);
	}


	CtrlKey::CtrlKey(SeqInput* sender, BYTE vk) : HitKey(sender)
	{
		DWORD flag = 0;
		switch(vk)
		{
		case VK_LEFT:
		case VK_RIGHT:
		case VK_UP:
		case VK_DOWN:
			flag = KEYEVENTF_EXTENDEDKEY;
			break;
		}
		KeyDown(vk, flag);
	}

}//namespace